from selenium import webdriver
from selenium.webdriver.common.by import By
from fake_useragent import UserAgent
from time import sleep
import re
import csv



def write_csv(data):

    """ Записать все файлы в csv файл """

    with open('wildberries.csv', 'a') as file:

        writer = csv.writer(file)

        writer.writerow((
            data['goods_name'],
            data['brand_name'],
            data['lower_price'],
            data['old_price'],
            data['link']
        ))



def set_settings():

    """ Настройка браузера """

    user_agent = UserAgent()
    ua = user_agent.opera

    options = webdriver.ChromeOptions()
    options.add_argument(f'--user-agent={ua}')
    options.add_argument('--disable-blink-features=AutomationControlled')
    #options.add_argument('--headless')

    browser = webdriver.Chrome(
        executable_path='/home/borya/foo/ide/selenium_/web_driver/chromedriver',
        options=options
    )

    return browser



def get_product_links(links):

    """ Получить все ссылки на товары """

    all_links = []

    for link in links:

        all_links.append(link.get_attribute('href'))

    return all_links



def get_product_lower_prices(lower_prices):

    """ Получить все цены с товаров """

    all_lower_prices = []

    for lower_price in lower_prices:

        price = re.findall(r'\d+', lower_price.text)

        if len(price) == 1: all_lower_prices.append(price[0])

        else:  all_lower_prices.append(price[0] + price[1])

    return all_lower_prices



def get_brand_name(brand_name):

    """ Получить все имена брендов товара """

    all_brand_name = []

    for name in brand_name:

        name = name.text.split(' ')[0]
        all_brand_name.append(name)

    return all_brand_name



def get_goods_name(goods_name):

    all_goods_name = []

    for name in goods_name:

        all_goods_name.append(name.text)

    return all_goods_name



def get_product_oldprices(old_prices):

    """ Получить старую цену товара """

    all_old_prices = []

    for old_price in old_prices:

        price = re.findall(r'\d+', old_price.text)
        price = price[0] + price[1]
        all_old_prices.append(price)

    return all_old_prices



def write_data(all_links, all_lower_prices, all_old_prices, all_brand_name, all_goods_names):

    """ Проходимся по всем собранным данным """

    link = (link for link in all_links)

    lower_price = (lower_price for lower_price in all_lower_prices)

    old_price = (old_price for old_price in all_old_prices)

    brand_name = (brand_name for brand_name in all_brand_name)

    goods_name = (goods_name for goods_name in all_goods_names)


    while True:

        data = {
            'goods_name': next(goods_name),
            'brand_name': next(brand_name),
            'lower_price': next(lower_price),
            'old_price': next(old_price),
            'link': next(link)
        }

        print(data)
        write_csv(data)



def get_all_products(browser):

    """ Получить все товары со страницы """

    all_products = browser.find_elements(
        By.XPATH,
        '//div[@id="catalog-content"]/div[4]/*'
    )


    for product in all_products:

        """ Пройтись по всем товарам со страницы """

        links = product.find_elements(
            By.XPATH,
            '//div[@id="catalog-content"]/div[4]//div[@class="l_class"]/parent::a'
        )
        all_links = get_product_links(links)


        lower_prices = browser.find_elements(
           By.XPATH,
            '//div[@id="catalog-content"]//div[4]//div[@class="l_class"]/following-sibling::div//div[@class="j-cataloger-price"]//ins'
        )
        all_lower_prices = get_product_lower_prices(lower_prices)


        old_prices = browser.find_elements(
            By.XPATH,
            '//div[@id="catalog-content"]//div[4]//div[@class="l_class"]/following-sibling::div//div[@class="j-cataloger-price"]//del'
        )
        all_old_prices = get_product_oldprices(old_prices)


        brand_name = browser.find_elements(
            By.XPATH,
            '//div[@id="catalog-content"]/div[4]//div[@class="l_class"]/following-sibling::div/div[@class="dtlist-inner-brand-name"]//strong'
        )
        all_brand_name = get_brand_name(brand_name)


        goods_name = browser.find_elements(
            By.XPATH,
            '//div[@id="catalog-content"]/div[4]//div[@class="l_class"]/following-sibling::div/div[@class="dtlist-inner-brand-name"]/span'
        )
        all_goods_name = get_goods_name(goods_name)

        write_data(all_links, all_lower_prices, all_old_prices, all_brand_name, all_goods_name)



def working_browser(browser, url):

    """ Работа с браузером """

    browser.get(url)
    browser.maximize_window()

    get_all_products(browser)



def main():

    url = 'https://www.wildberries.ru/brands/vans'

    browser = set_settings()
    working_browser(browser, url)



if __name__ == '__main__':

    main()
